package com.edu.networkexperimentation.controller;


import com.edu.networkexperimentation.common.BaseResponse;
import com.edu.networkexperimentation.common.ResultUtils;
import com.edu.networkexperimentation.mapper.RelationMapper;
import com.edu.networkexperimentation.model.domain.Effect;
import com.edu.networkexperimentation.model.domain.Relation;
import com.edu.networkexperimentation.model.request.FakeData;
import com.edu.networkexperimentation.model.request.RequestAnswer;
import com.edu.networkexperimentation.model.request.RequestPaperGenetic;
import com.edu.networkexperimentation.model.response.ResponseAlgorithmTableData;
import com.edu.networkexperimentation.model.response.ResponseHistoryPaper;
import com.edu.networkexperimentation.model.response.ResponsePaper;
import com.edu.networkexperimentation.model.response.ResponsePaperBJ;
import com.edu.networkexperimentation.service.EffectService;
import com.edu.networkexperimentation.service.GeneticService;
import com.edu.networkexperimentation.service.PaperService;
import com.edu.networkexperimentation.service.ProcessService;
import com.edu.networkexperimentation.service.impl.EffectServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("paper")
@Slf4j
public class PaperController {

    @Resource
    private PaperService paperService;


    @Resource
    private GeneticService geneticService;
    @Resource
    private EffectService effectService;
    @Resource
    private RelationMapper relationMapper;
    @Resource
    private ProcessService processService;
//
//    @PostMapping("/prepare")
//    public BaseResponse<ResponsePaper> preparePaper(@RequestBody RequestPaperGenetic paper) {
//        return ResultUtils.success(paperService.preparePaper(paper, System.currentTimeMillis()));
//    }

    @PostMapping("/submit")
    public BaseResponse<Boolean> submitPaper(@RequestBody List<RequestAnswer> answers) {
        log.info("答案的个数: {}", answers.size());
        paperService.submitPaper(answers);
        return ResultUtils.success(true);
    }

    @GetMapping("/user/{id}")
    public BaseResponse<List<ResponseHistoryPaper>> getAllHistoryPaperByUserID(@PathVariable("id") Long id) {
        return ResultUtils.success(paperService.getAllPaperByUserID(id));
    }

    @GetMapping("/info/{id}")
    public BaseResponse<ResponseHistoryPaper> getHistoryPaperByID(@PathVariable("id") Long id) {
        return ResultUtils.success(paperService.getPaperByID(id));
    }

    @GetMapping("/algo/info/{id}")
    public BaseResponse<ResponsePaperBJ> getHistoryPaperByAlgorithmID(@PathVariable("id") Long id) {
        List<Effect> effects = effectService.getEffectsByPaperID(id);
        ResponsePaperBJ responsePaperBJ = new ResponsePaperBJ();
        responsePaperBJ.setEffects(effects);
        return ResultUtils.success(responsePaperBJ);
    }

    @GetMapping("/algo/table/{id}")
    public BaseResponse<ResponseAlgorithmTableData> getHistoryPaperTableByAlgorithmID(@PathVariable("id") Long id) {
        ResponseAlgorithmTableData data = processService.getAlgorithmTableData(id);
        return ResultUtils.success(data);
    }

    @PostMapping("/getPaper")
    public BaseResponse<ResponsePaper> getAllPaper(@RequestBody RequestPaperGenetic requestPaperGenetic) {
//        return ResultUtils.success(timesMapper.selectList(null));
        long r_flag = System.currentTimeMillis();
        ResponsePaper paper0 = paperService.preparePaperByNewGenetic(requestPaperGenetic, r_flag);
        ResponsePaper paper1 = paperService.preparePaperAgain(requestPaperGenetic, r_flag);
        ResponsePaper paper2 = paperService.preparePaperByGenetic(requestPaperGenetic, r_flag);
        ResponsePaper paper = null;
        double[] nums = new double[3];
        nums[0] = paper0.getFitness();
        nums[1] = paper1.getFitness();
        nums[2] = paper2.getFitness();
        int i = 0;
        double flag = 0;
        for (int j = 0; j < nums.length; j++) {
            if (nums[j] > flag) {
                flag = nums[j];
                i = j;
            }
        }
        switch (i) {
            case 0:
                paper = paper0;
                break;
            case 1:
                paper = paper1;
                break;
            default:
                paper = paper2;
        }

        Relation relation = new Relation();
        relation.setPaperid(paper0.getId());
        relation.setFlag(r_flag);
        relationMapper.insert(relation);
        relation.setId(null);
        relation.setPaperid(paper1.getId());
        relation.setFlag(r_flag);
        relationMapper.insert(relation);
        relation.setId(null);
        relation.setPaperid(paper2.getId());
        relation.setFlag(r_flag);
        relationMapper.insert(relation);

        return ResultUtils.success(paper);
    }

    @PostMapping("/fake")
    public BaseResponse<Boolean> makeFakeData(@RequestBody FakeData fakeData) {
        return ResultUtils.success(geneticService.makeFakeData(fakeData.getNum()));
    }
}
